﻿using Face8280WebSocketServer.DB.Device;
using Face8280WebSocketServer.Interface;
using Face8280WebSocketServer.Utility;
using Face8280WebSocketServer.Utility.JWT;
using Face8280WebSocketServer.Utility.Model;
using Face8280WebSocketServer.Utility.ExpressionExtend;
using Face8280WebSocketServer.Controllers.User;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Face8280WebSocketServer.Utility.FilterAttribute;
using Newtonsoft.Json;
using Face8280WebSocketServer.DB.System;
using Face8280WebSocketServer.DB.TimeGroup;
using Face8280WebSocketServer.DTO;

namespace Face8280WebSocketServer.Controllers.FaceDevice
{
    [Route("api/[controller]")]
    [ApiController]
    public class TimeGroupController : ControllerBase
    {

        private readonly ILogger<FaceDeviceController> _logger;
        private IMyMemoryCache _Cache;
        private ITimeGroupService _TimeGroupDB;

        public TimeGroupController(ILogger<FaceDeviceController> logger,
            ITimeGroupService door, IMyMemoryCache mycache)
        {
            _logger = logger;
            _TimeGroupDB = door;
            _Cache = mycache;
        }


        /// <summary>
        /// 获取开门时段列表
        /// </summary>
        /// <param name="par"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("GetList")]
        [Authorize]
        public IActionResult GetList([FromForm] BasePageParameter par)
        {
            //_logger.LogInformation("获取开门时段列表");
            var oTimeGroups = _TimeGroupDB.QueryPage<TimeGroupDetail, int>(
                null, par.pageSize, par.pageIndex,
                x => x.Num,
                true);

            return new JsonResult(new JsonResultModel(oTimeGroups));
        }


        /// <summary>
        /// 更新开门时段
        /// </summary>
        /// <param name="par"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("Update")]
        [Authorize]
        public IActionResult Update([FromForm] TimeGroupDetail par)
        {
            if(par.Num == 1)
            {
                return new JsonResult(new JsonResultModel(10,"时段1不可更改"));
            }
            _TimeGroupDB.UpdateTimeGroup(par);

            return new JsonResult(new JsonResultModel());
        }

        


        /// <summary>
        /// 恢复默认值
        /// </summary>
        /// <param name="par"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("SetDefault")]
        [Authorize]
        public IActionResult SetDefault()
        {
            _TimeGroupDB.IniTimeGroupDB();

            return new JsonResult(new JsonResultModel());
        }

    }
}
